<?php

class Result {

    public $siteUrl;
    public $name;
    private $cat_name;
    private $art;
    private $price;
    private $desc;
    private $har;
    private $imgs;
    private $id;
    private $search;
    private $replace;

    public function __construct(MyMultiCurl $parser) {
        $this->siteUrl = $parser->siteUrl;
        $this->name = $parser->name;
        $this->cat_name = $parser->cat_name;
        $this->art = $parser->art;
        $this->price = $parser->price;
        $this->desc = $parser->desc;
        $this->har = $parser->har;
        $this->imgs = $parser->imgs;
        $this->id = $parser->id;
        $this->search = $parser->search;
        $this->replace = $parser->replace;
    }

    #сохраняет картинки

    private function saveImg($img_path) {
        $img_path = str_replace(" ", "%20", $img_path);
        if (strpos($img_path, parse_url($this->siteUrl, PHP_URL_SCHEME)) !== 0) {
            $img_path = $this->siteUrl . $img_path;
        }
        $img_name = end(explode("/", $img_path));
        $dir = "tovar_img" . DIRECTORY_SEPARATOR . parse_url($this->siteUrl, PHP_URL_HOST) . DIRECTORY_SEPARATOR;
        #если указали что заменять в урле для большой картинки то её тоже сохраняем
        if ($this->search and $this->replace) {
            $big_img_path = str_replace($this->search, $this->replace, $img_path);
            $big_img_name = end(explode("/", $big_img_path));
            $r = copy($big_img_path, $dir . DIRECTORY_SEPARATOR . $big_img_name);          
        }
        if (!@copy($img_path, $dir . DIRECTORY_SEPARATOR . $img_name)) {
            @copy($img_path, $dir . DIRECTORY_SEPARATOR . $img_name);
        }
        if ($r == 1) {
            return $img_name . ";" . $big_img_name;
        } else {
            return $img_name;
        }
    }

    #записывает в CSV

    public function add() {
        $file = fopen("result.csv", "a");
        foreach ($this->imgs as $img) {
            $img_name .= $this->saveImg($img) . " ";
        }
        //echo "Категория".$this->cat_name[$this->id] . ";" . "Название".$this->name . ";" . "Артикул".$this->art . ";" . "Цена".$this->price . ";" . "Описание".$this->desc . ";" . $this->har . ";" . $img_name . "\r\n"; die();
        fwrite($file, $this->cat_name[$this->id] . ";" . $this->name . ";" . $this->art . ";" . $this->price . ";" . $this->desc . ";" . $this->har . ";" . $img_name . "\r\n");
        fclose($file);
    }

    public static function createZip($site_url) {
        $archive_name = dirname(__FILE__) . DIRECTORY_SEPARATOR . "tovar_img" . DIRECTORY_SEPARATOR . parse_url($site_url, PHP_URL_HOST) . DIRECTORY_SEPARATOR . "images.zip";
        //$archive_name = $archive_dir . parse_url($this->siteUrl, PHP_URL_HOST) . ".zip";
        $src_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . "tovar_img" . DIRECTORY_SEPARATOR . parse_url($site_url, PHP_URL_HOST);
        $zip = new ZipArchive();
        if ($zip->open($archive_name, ZIPARCHIVE::CREATE) !== true) {
            die("error zip");
        }

        $dirHandle = opendir($src_dir);
        while (false !== ($file = readdir($dirHandle))) {
            if ($file != "." and $file != ".." and $file != "images.zip") {
                $file_path = $src_dir . DIRECTORY_SEPARATOR . $file;
                $zip->addFile($file_path, $file);
            }
        }
        $zip->addFile("result.csv", "result.csv");
        $zip->close();
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="images.zip"');
        readfile($archive_name);
    }

}

?>
